package com.ruoyi.project.xcx.myhome.goods.api;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.xcx.myhome.goods.domain.XcxMyhomeGoods;
import com.ruoyi.project.xcx.myhome.goods.dto.GoodsDto;
import com.ruoyi.project.xcx.myhome.goods.service.IXcxMyhomeGoodsService;
import com.ruoyi.project.xcx.user.domain.XcxUser;
import com.ruoyi.project.xcx.util.TokenUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/wx/goods")
@Slf4j
public class GoodsController {
    @Autowired
    private IXcxMyhomeGoodsService xcxMyhomeGoodsService;

    /**
     * 即可根据物品名称查询，也可以根据类别名称查询
     * @param goods
     * @return
     */
    @PostMapping("listGoods")
    public AjaxResult listGoods(@RequestBody GoodsDto goods){
        XcxUser user = TokenUserUtil.getUser();
        if(goods != null && StringUtils.equalsIgnoreCase(goods.getSearchName(), "null")){
            goods.setSearchName(null);
        }
        if(goods != null && StringUtils.equalsIgnoreCase(goods.getSearchType(), "null")){
            goods.setSearchType(null);
        }
        return AjaxResult.success(xcxMyhomeGoodsService.listGoods(goods, user));
    }

    /**
     * 获取某个物品信息
     * @param id
     * @return
     */
    @GetMapping("getGoods")
    public AjaxResult getGoods(Long id){
        XcxUser user = TokenUserUtil.getUser();
        if(id == null){
            return AjaxResult.error("参数不能为空");
        }
        GoodsDto goodsDto = xcxMyhomeGoodsService.getGoods(id, user);
        if(goodsDto == null){
            return AjaxResult.error("物品不存在");
        }else{
            return AjaxResult.success("成功", goodsDto);
        }
    }

    /**
     * 新增物品
     * @param goods
     * @return
     */
    @PostMapping("addGoods")
    @Log(title = "小程序新增物品", businessType = BusinessType.INSERT)
    public AjaxResult addGoods(@RequestBody GoodsDto goods){
        XcxUser user = TokenUserUtil.getUser();
        AjaxResult validate = addGoodsValidate(goods);
        if(validate != null){
            return validate;
        }
        xcxMyhomeGoodsService.addGoods(goods, user);
        return AjaxResult.success("成功");
    }
    /**
     * 修改保存myhome物品
     */
    @Log(title = "小程序修改物品", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult editSave(@RequestBody GoodsDto goods) {
        XcxUser user = TokenUserUtil.getUser();
        AjaxResult validate = editSaveValidate(goods);
        if(validate != null){
            return validate;
        }
        xcxMyhomeGoodsService.editGoods(goods, user);
        return AjaxResult.success();
    }

    /**
     * 删除myhome物品
     */
    @Log(title = "小程序删除物品", businessType = BusinessType.DELETE)
    @GetMapping( "/remove")
    public AjaxResult remove(Long id) {
        XcxUser user = TokenUserUtil.getUser();
        xcxMyhomeGoodsService.deleteGoods(id, user);
        return AjaxResult.success();
    }
    @Log(title = "小程序减少物品", businessType = BusinessType.UPDATE)
    @GetMapping( "/decrease")
    public AjaxResult decrease(Long id) {
        XcxUser user = TokenUserUtil.getUser();
        xcxMyhomeGoodsService.decreaseGoods(id, user);
        return AjaxResult.success();
    }

    /**
     * 新增物品校验
     * @param goods
     * @return
     */
    private AjaxResult addGoodsValidate(GoodsDto goods){
        if(goods == null){
            return AjaxResult.error("物品名称不能为空");
        }
        if(StringUtils.isBlank(goods.getCategoryName())){
            goods.setCategoryName("其他");
        }
        if(StringUtils.isBlank(goods.getName())){
            return AjaxResult.error("物品名称不能为空");
        }
        if(goods.getCount() == null){
            goods.setCount(1L);
        }
        return null;
    }

    /**
     * 修改物品校验
     * @param goods
     * @return
     */
    private AjaxResult editSaveValidate(GoodsDto goods){
        if(goods == null){
            return AjaxResult.error("物品id不能为空");
        }
        if(goods.getId() == null){
            return AjaxResult.error("物品id不能为空");
        }
        if(StringUtils.isBlank(goods.getName())){
            return AjaxResult.error("物品名称不能为空");
        }
        if(StringUtils.isBlank(goods.getCategoryName())){
            goods.setCategoryName("其他");
        }
        if(goods.getCount() == null){
            goods.setCount(1L);
        }
        return null;
    }
}
